package com.message.api.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.base.helper.StringUtils;
import com.message.api.base.entity.MsNotice;
import com.message.api.base.service.MsNoticeService;
import com.message.api.base.vo.MsNoticeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-08-10
 */
@Api(tags = "通知公告")
@RestController
@RequestMapping("/msNotice")
public class MsNoticeController extends BaseController {

    private static final Logger          LOG = LoggerFactory.getLogger(MsNoticeController.class);
    @Autowired
    private              MsNoticeService entityService;

    @SysLog(action = "msNotice:findByPage", value = "公告管理：公告列表")
    @ApiOperation(value = "分页查询通知公告", notes = "分页查询通知公告")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<MsNoticeVo>> findByPage(final MsNoticeVo example, final Page page) {
        IPage<MsNoticeVo> records = entityService.findByPage(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "msNotice:delete", value = "公告管理：删除公告")
    @ApiOperation(value = "删除通知公告")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "通知公告ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        entityService.removeById(id);
        return new Result(true, "成功删除通知公告", null);
    }

    @SysLog(action = "msNotice:one", value = "公告管理：公告详情")
    @ApiOperation(value = "查询单个通知公告")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "通知公告ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        MsNotice entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "msNotice:add", value = "公告管理：添加公告")
    @ApiOperation(value = "添加通知公告")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final MsNoticeVo entity) {
        if (StringUtils.isEmpty(entity.getTextContent())) {
            throw new RuntimeException("公告内容不能为空");
        }
        if (null == entity.getId()) {
            entity.setCreatetime(new Date());
            entity.setTop("0");
            entity.setState("1");
        }
        entityService.saveNotice(entity);
        return new Result(true, "成功保存通知公告", null);
    }

    @SysLog(action = "msNotice:update", value = "公告管理：修改公告")
    @ApiOperation(value = "修改通知公告")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final MsNotice entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改通知公告", null);
    }

    @SysLog(action = "msNotice:all", value = "公告管理：全部通知公告")
    @ApiOperation(value = "全部通知公告")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<MsNoticeVo>> all(MsNoticeVo example) {
        List<MsNoticeVo> entitys = entityService.findByExample(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

    @SysLog(action = "msNotice:setTop", value = "公告管理：设置公告置顶")
    @ApiOperation(value = "设置置顶状态")
    @RequestMapping(value = "{id}/setTop/{top}", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "top", value = "置顶状态：1启用，0取消", required = true, paramType = "path"
    )})
    public Result<MsNotice> setTop(@PathVariable(name = "id") final Integer id,
                                   @PathVariable(name = "top") String top) {
        if ("1".equals(top) || "0".equals(top)) {
            MsNotice msNotice = entityService.setTop(id, top);
            return new Result<>(true, msNotice);
        } else {
            return new Result(false, "设置置顶状态失败", "设置参数非法，只能设置为0或1");
        }
    }

}

